package com.youlu.campus.service.activity.impl;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.entity.ActivityPointConfig;
import com.youlu.campus.entity.PointPlatformConfig;
import com.youlu.campus.entity.VO.req.ActivityPointConfigQueryVO;
import com.youlu.campus.service.activity.ActivityPointConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ActivityPointConfigServiceImpl implements ActivityPointConfigService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean create(ActivityPointConfig req) {
        req.setCreatedTime(new Date());
        req.setId(null);
        mongoTemplate.insert(req);
        return true;
    }

    @Override
    public boolean update(@NotNull ActivityPointConfig req) {
        if (StringUtils.isBlank(req.getId())) {
            log.error(":>>> 修改活动积分配置参数错误:ID为空");
            return false;
        }
        Query query = new Query().addCriteria(Criteria.where("id").is(req.getId()));
        Update update = new Update();
        boolean toUpdate = false;
        if (Objects.nonNull(req.getRules())) {
            update.set("rules", req.getRules());
            toUpdate = true;
        }
        if (Objects.nonNull(req.getTotalPoint())) {
            update.set("totalPoint", req.getTotalPoint());
            toUpdate = true;
        }
        if (toUpdate) {
            UpdateResult updateResult = mongoTemplate.updateFirst(query, update, ActivityPointConfig.class);
            return updateResult.getModifiedCount() > 0 ? true : false;
        }
        return false;
    }

    @Override
    public boolean delete(String id) {
        Query query = new Query().addCriteria(Criteria.where("id").is(id));
        DeleteResult deleteResult = mongoTemplate.remove(query, ActivityPointConfig.class);
        return deleteResult.getDeletedCount() > 0 ? true : false;
    }

    @Override
    public ActivityPointConfig findByActivityId(String activityId) {
        Query query = new Query().addCriteria(Criteria.where("activityId").is(activityId));
        return mongoTemplate.findOne(query, ActivityPointConfig.class);
    }

    @Override
    public Page<ActivityPointConfig> list(@NotNull ActivityPointConfigQueryVO req) {
        log.info(":>>> 开始获取活动积分配置:{}", JSON.toJSONString(req));
        Query query = new Query();

        if (StringUtils.isNotBlank(req.getActivityId())) {
            query.addCriteria(Criteria.where("activityId").is(req.getActivityId()));
        }
        query.addCriteria(Criteria.where("deleted").ne(true));
        long count = mongoTemplate.count(query, ActivityPointConfig.class);
        PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        Sort sort=Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sort);
        List<ActivityPointConfig> infos = mongoTemplate.find(query.with(pageRequest), ActivityPointConfig.class);
        return PageableExecutionUtils.getPage(infos, pageRequest, () -> count);

    }

    @Override
    public ActivityPointConfig findOne(String id) {
        Query query = new Query().addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, ActivityPointConfig.class);
    }

    @Override
    public Integer getPoint(String activityId, String bizType) {
        Query query = new Query().addCriteria(Criteria.where("activityId").is(activityId));
        ActivityPointConfig config = mongoTemplate.findOne(query, ActivityPointConfig.class);
        if (Objects.isNull(config)) {
            log.error(":>>> 活动没有配置积分规则");
            return 1000;
        }
        if (config.getRules().containsKey(bizType)) {
            return config.getRules().get(bizType);
        }
        return 1000;
    }
}
